<template>
  <div class="register-container">
    <div class="register-bg">
      <h1 class="banner-h1">曲稻劳务派遣系统</h1>
      <div class="banner">
        <img src="@/assets/images/work_schedule_2.png" />
        <h2 class="banner-h2-left">项目全周期可视化 管控面面俱到</h2>
        <h2 class="banner-h2-right">电子合同便捷签署 数字化合同管理</h2>
      </div>
    </div>

    <div class="register-form">
      <el-form
        ref="registerForm"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">曲稻劳务派遣系统开通申请</h3>
        </div>
        <el-form-item prop="mobile">
          <el-input
            ref="mobile"
            v-model="registerForm.mobile"
            placeholder="手机号"
            name="mobile"
            type="text"
            tabindex="0"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="ProxyOrganizationName">
          <el-input
            ref="ProxyOrganizationName"
            v-model="registerForm.ProxyOrganizationName"
            placeholder="请输入企业名称"
            name="ProxyOrganizationName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="ProxyOperatorName">
          <!-- <span class="svg-container">
            <svg-icon icon-class="user" />
          </span> -->
          <el-input
            ref="ProxyOperatorName"
            v-model="registerForm.ProxyOperatorName"
            placeholder="请输入经办人姓名"
            name="ProxyOperatorName"
            type="text"
            tabindex="2"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="UniformSocialCreditCode">
          <el-input
            ref="UniformSocialCreditCode"
            v-model="registerForm.UniformSocialCreditCode"
            placeholder="请输入企业统一社会信用代码"
            name="UniformSocialCreditCode"
            tabindex="3"
            auto-complete="on"
          />
        </el-form-item>
        <span
          ><el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleRegister"
            >申请开通</el-button
          ></span
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@common/utils/validate';
// import { userSync } from '@common/api/staff';

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'));
      } else {
        callback();
      }
    };

    return {
      registerForm: {
        mobile: '',
        ProxyOrganizationName: '',
        ProxyOperatorName: '',
        UniformSocialCreditCode: '',
      },
      registerRules: {
        mobile: [{ required: true, message: '请输入手机号' }],
        ProxyOrganizationName: [
          { required: true, message: '请输入企业名称' },
          { validator: validateUsername, message: '请输入正确的企业名称' },
          { max: 64, message: '企业名称最大长度为64' },
        ],
        ProxyOperatorName: [
          { required: true, message: '请输入经办人姓名' },
          { validator: validateUsername, message: '请输入正确的经办人姓名' },
          { max: 50, message: '企业名称最大长度为50' },
        ],
        UniformSocialCreditCode: [
          { required: true, message: '请输入企业统一社会信用代码' },
          { max: 200, message: '企业统一社会信用代码最大长度为200' },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  created() {
    // userSync();
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    handleLogin() {
      this.redirect = true;
      this.$router.push({ path: '/login' });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('user/register', this.registerForm)
            .then(data => {
              this.loading = false;
              if (data.ConsoleUrl) {
                this.loading = false;
                if (!data.IsActivated || !data.ProxyOperatorIsVerified) {
                  // 企业未激活或经办人未完成认证,跳转到控制台
                  window.open(data.ConsoleUrl, '_self');
                } else {
                  this.$router.push({ path: '/staff/index' }); // 完成认证的企业，直接进入首页
                }
              }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  display: flex;
  .register-form {
    width: 60%;
  }
  .register-bg {
    width: 40%;
    background-color: white;
    display: flex;
    align-items: center;
    flex-direction: column;
    .banner-h1 {
      text-align: left;
      position: fixed;
      left: 20px;
      top: 20px;
      margin: 0;
    }
    .banner {
      width: 500px;
      padding-top: 300px;

      img {
        width: 100%;
      }

      .banner-h2-left {
        text-align: left;
        margin: 20px 0 10px;
      }
      .banner-h2-right {
        text-align: right;
        margin: 20px 0 10px;
      }
    }
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .no-border {
    border: none;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.register-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
